package com.systemframework.cos;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import com.systemframework.constants.Constants;
import com.systemframework.exception.ResultException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hpsf.ReadingNotSupportedException;
import org.checkerframework.checker.index.qual.SameLen;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.Date;

/**
 * @author summer
 * @date 2024-09-19 19:01
 **/
@Configuration
@ConfigurationProperties(prefix = "cos")
@Data
@Slf4j
public class CosConfigure {

  private String secretId;

  private String secretKey;

  private String bucketName;

  private String region;

  private String access;

  public COSClient cosClient(){
    COSCredentials cred = new BasicCOSCredentials(secretId,secretKey);
    ClientConfig clientConfig = new ClientConfig(new Region(region));
    return new COSClient(cred, clientConfig);
  }


  public String putFile(MultipartFile file){
    InputStream inputStream =null;
    try {
      if(StrUtil.isEmpty(file.getOriginalFilename())){
        throw new ResultException("文件不能为空");
      }
      inputStream = file.getInputStream();
      COSClient cosClient = cosClient();
      String format = DateUtil.format(new Date(), Constants.DATE_TIME_FORMAT)+Constants.SLASH+file.getOriginalFilename();
      ObjectMetadata objectMetadata = new ObjectMetadata();
      objectMetadata.setContentType(file.getContentType());
      objectMetadata.setContentLength(file.getSize());
      cosClient.putObject(bucketName, format, inputStream, objectMetadata);
      cosClient.shutdown();
      return access+format;
    }catch (Exception e){
      throw new ResultException(e.getMessage());
    }finally {
      try {
        if(inputStream != null){
          inputStream.close();
        }
      }catch (Exception e){
        log.error(e.getMessage());
      }

    }
  }

  public String putFile(InputStream inputStream){
    try {
      COSClient cosClient = cosClient();
      String format = DateUtil.format(new Date(), Constants.DATE_TIME_FORMAT)+Constants.SLASH+ IdUtil.fastSimpleUUID()+Constants.IMAGE_TYPE_PNG;
      ObjectMetadata objectMetadata = new ObjectMetadata();
      BufferedInputStream bufferedInputStream=new BufferedInputStream(inputStream);
      long length=0;
      while (bufferedInputStream.skip(1)>0){
        length++;
      }
      objectMetadata.setContentLength(length);
      cosClient.putObject(bucketName, format, inputStream, objectMetadata);
      cosClient.shutdown();
      return access+format;
    }catch (Exception e){
       log.error(e.getMessage());
       throw new ResultException(e.getMessage());
    }finally {
      try {
        if(inputStream != null){
          inputStream.close();
        }
      }catch (Exception e){
        log.error(e.getMessage());
      }

    }
  }
}
